//index.js
var util = require('../../utils/util.js')
var app = getApp()
Page({
    /**
     * 页面的初始数据
     */
    data: {
        pmup: '08:30',
        pmdown: '12:30',
        amup: '14:30',
        amdown: '18:30',
        latitude: 28.6756300000,
        longitude: 115.9944400000,
        no: '您已经打卡',
        message: '',
        time: '',
        times: '',
        stytleimg: 'up.png',
        stytle: '#ccc',
        hidden: true,
        nocancel: true,
        dataInfo0: { 'status': '', 'time': '' },
        dataInfo1: { 'status': '', 'time': '' },
        dataInfo2: { 'status': '', 'time': '' },
        dataInfo3: { 'status': '', 'time': '' },

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function() {
        this.getUserInfo();
        this.getDetail();
        this.natime();
    },
    onShow: function() {
        this.getUserInfo();
        util.checkLogin();
        let setInter = setInterval(() => {
            // this.natime();
            this.getLocations();
        }, 3000)
        setTimeout(function() {
            clearInterval(setInter)
            // this.getLocations();
        });
    },

    //获取当天打卡信息
    getDetail: function() {
        var arrs = util.getNowDatas();
        util.myrequest(arrs, '/checkin/lists', 'GET', (res) => {
            if (res.result) {
                if (res.datas.length >= 1) {
                    this.setData({
                        dataInfo0: res.datas[0]
                    })
                }
                if (res.datas.length >= 2) {
                    this.setData({
                        dataInfo1: res.datas[1]
                    })
                }
                if (res.datas.length >= 3) {
                    this.setData({
                        dataInfo2: res.datas[2]
                    })
                }
                if (res.datas.length >= 4) {
                    this.setData({
                        dataInfo3: res.datas[3]
                    })
                }
            }
        })
    },

    //获取员工信息
    getUserInfo: function() {
        util.myrequest("", '/staff/info', 'GET', (res) => {
            console.log(res)
            if (res.result) {
                this.setData({
                    userInfo: res.datas
                })
                // console.log(this.userInfo)                
            }
        })
    },

    // 获取位置
    getLocations: function() {
        wx.getLocation({
            type: 'wgs84',
            success: (res) => {
                var latitude = res.latitude
                var longitude = res.longitude
                var speed = res.speed
                var accuracy = res.accuracy
                this.setData({
                    latitude: latitude,
                    longitude: longitude,
                    
                    markers: [{
                            latitude: 28.6756300000,
                            longitude: 115.9944400000,
                            title: '上班的位置',
                            address: '紫阳明珠25楼',
                            iconPath: '../images/s.png',
                            width: 25,
                            height: 25
                        },
                        {
                            latitude: latitude,
                            longitude: longitude,
                            title: '上班的位置',
                            address: '紫阳明珠23楼',
                            iconPath: '../images/s.png',
                            width: 25,
                            height: 25
                        },
                        console.log(111)
                    ],
                    circles: [{
                        fillColor: "#0079ffa0",
                        color: '#00000000',
                        latitude: 28.6756300000,
                        longitude: 115.9944400000,
                        radius: 100,
                        strokeWidth: 1
                    }],
                    polyline: [{
                        points: [{
                            latitude: 28.6756300000,
                            longitude: 115.9944400000,
                        }, {
                            latitude: latitude,
                            longitude: longitude,
                        }],
                        color: "#FF0000DD",
                        width: 2,
                        dottedLine: true
                    }],

                })
            }
        })
    },

    // 获取时间
    natime: function() {
        // 调用函数时，传入new Date()参数，返回值是日期和时间  
        var times = util.formatTime1(new Date());
        // 再通过setData更改Page()里面的data，动态更新页面的数据  
        this.setData({
            times: times
            
        });
        // console.log(times)
    },

    // 接口调用
    getList: function(hlat, hing, slat, sing, id) {
        var arrs = {
            id: 2,
            hlat: this.data.latitude, //展馆纬度
            hlng: this.data.longitude, //展馆经度
            slat: 28.6828300000, //员工纬度
            slng: 115.8584400000, //员工经度
        }
        util.myrequest(arrs, '/checkin/add', 'POST', (res) => {
            if (res.result) {
                this.getDetail();
                wx.showToast({
                    title: res.message,
                    icon: 'success',
                    duration: 800
                })
            } else {
                wx.showToast({
                    title: res.message,
                    icon: 'success',
                    duration: 800
                })
            }

        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {},
})